﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Asxsyd92Core.Utils;
using WebOS.Bll;
using WebOS.Dal;
using WebOS.Modle;
using WebOS.Modle.WorkFlowCustomEventParams;
using WebOS.Modle.WorkFlowInstalledSub;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace WebOSCore.Controllers.Api
{

    public class WorkFlowTasksController : Controller
    {
        [Route("api/WorkFlowTasks/WaitList")]
        [HttpGet]
        [Authorize]
        public string WaitList(string title, Guid type, Guid user, DateTime date1, DateTime date2, int page, int limit)
        {
            //根据用户获取待办
            var id = User.Claims.FirstOrDefault(c => c.Type == "id").Value;
            Guid guid;
            Guid.TryParse(id, out guid);
            return WorkFlowTaskBll.Instance.WaitList(guid, title, type, user, date1, date2, page, limit);

        }

        [Route("api/WorkFlowTasks/CompletedList")]
        [HttpGet]
        [Authorize]
        public string CompletedList(string title, Guid type, Guid user, DateTime date1, DateTime date2, int page, int limit)
        {
            //根据用户获取待办
            var id = User.Claims.FirstOrDefault(c => c.Type == "id").Value;
            Guid guid;
            Guid.TryParse(id, out guid);
            return WorkFlowTaskBll.Instance.CompletedList(guid, title, type, user, date1, date2, page, limit);

        }

        [Route("api/WorkFlowTasks/TasksDetail")]
        [HttpGet]
        [Authorize]
        public string TasksDetail(string InstanceID, Guid FlowID, Guid StepID)
        {
            //根据用户获取待办
            var flow = WorkFlowDal.Instance.Get(FlowID);
            // RoadFlow.Data.Model.WorkFlowInstalled wfInstalled = new RoadFlow.Data.Model.WorkFlowInstalled();
            var json = LitJSONCore.JsonMapper.ToObject(flow.RunJSON);
            var dbs = json["databases"];
            var Table = "";
            if (dbs.IsArray)
            {
                foreach (LitJSONCore.JsonData db in dbs)
                {
                    Table = db["table"].ToString();
                }
            }
            var user = User.Claims.FirstOrDefault(c => c.Type == "id").Value;
            Guid guid;
            Guid.TryParse(user, out guid);
            return WorkFlowTaskBll.Instance.TasksDetail(InstanceID, guid, FlowID, StepID, Table);
        }
        //初始化flows
        [Route("api/WorkFlowTasks/FlowInit")]
        [HttpPost]
        [Authorize]
        public string FlowInit(string FlowID, string stepid)
        {
            try
            {
                var wfl = WebOS.Dal.WorkFlowDal.Instance.Get(FlowID.ToGuid());
                string error = "";
                var wfInstalled = wfl.runjson;
                //WorkFlowBll.Instance.GetWorkFlowRunModel(wfl.RunJSON, out error);

                if (wfl == null)
                {
                    //  Response.Write("未找到流程运行时!");

                }
                else if (wfl.Status == 3)
                {
                    return JSONhelper.ToJson(new { code = 0, msg = "该流程已被卸载,不能发起新的流程实例", count = 0, data = new { } }, false);
                }
                else if (wfl.Status == 4)
                {
                    return JSONhelper.ToJson(new { code = 0, msg = "该流程已被删除,不能发起新的实例", count = 0, data = new { } }, false);

                }
                Guid stepID;
                if (!stepid.IsGuid(out stepID))
                {
                    stepID = wfInstalled.steps.First().id.ToString().ToGuid();
                }

                var currentStep = wfInstalled.steps.ToList().Find(p => p.id.ToGuid() == stepID);
                var nextSteps = WorkFlowBll.Instance.GetNextSteps(wfInstalled.id.ToGuid(), currentStep.id.ToGuid()).OrderBy(p => p.Position_x).ThenBy(p => p.Position_y).ToList();


                return JSONhelper.ToJson(new { code = 0, msg = "成功", count = 1, data = nextSteps, currentStep = currentStep }, false);

            }
            catch (Exception ex) {
                return JSONhelper.ToJson(new { code = 0, msg = "未找到流程步骤", count = 0, data = "", currentStep = "" }, false);
            }

        }
    }
}
